import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'Index',
        redirect: '/home', // 重定向:重新指向其它path,会改变网址
    }, {
        path: '/home',
        name: 'Home',
        component: Home,
        meta: {
            title: '首页',
        },
        children: [{
            path: 'homePage',
            name: 'HomePage',
            component: () =>
                import ( /* webpackChunkName: "about" */ '../views/Home/HomePage.vue'),
            meta: {
                title: '首页',
                // <van-icon name="wap-home-o" />
                icon: 'wap-home-o'
            }
        }, {
            path: 'clissify',
            name: 'Clissify',
            component: () =>
                import ( /* webpackChunkName: "about" */ '../views/Home/Clissify.vue'),
            meta: {
                title: '分类',
                // <van-icon name="wap-home-o" />
                icon: 'wap-home-o'
            }
        }, {
            path: 'surprise',
            name: 'Surprise',
            component: () =>
                import ( /* webpackChunkName: "about" */ '../views/Home/Surprise.vue'),
            meta: {
                title: '京喜',
                // <van-icon name="wap-home-o" />
                icon: 'wap-home-o'
            }
        }, {
            path: 'shopCar',
            name: 'ShopCar',
            component: () =>
                import ( /* webpackChunkName: "about" */ '../views/Home/ShopCar.vue'),
            meta: {
                title: '购物车',
                // <van-icon name="wap-home-o" />
                icon: 'wap-home-o'
            }
        }, {
            path: 'my',
            name: 'My',
            component: () =>
                import ( /* webpackChunkName: "about" */ '../views/Home/My.vue'),
            meta: {
                title: '我的',
                // <van-icon name="wap-home-o" />
                icon: 'wap-home-o'
            }
        }]
    },
    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/Login.vue'),
        meta: {
            title: '登录',
        },
    }, {
        path: '/reg',
        name: 'Reg',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/Reg.vue'),
        meta: {
            title: '登录',
        },
    }, {
        path: '/detail/:id',
        name: 'Detail',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/Detail.vue'),
        meta: {
            title: '详情',
        },

    }, {
        path: '/notFound',
        name: 'NotFound',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/NotFound.vue'),
        meta: {
            title: '404',
        },

    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 访问的白名单, 可以直接通行
router.beforeEach((to, from, next) => {
    // 访问的路径在白名单
    document.title = to.meta.title
    if (to.name == 'My') {
        const loginState = localStorage.getItem('loginState');
        if (!loginState) {
            next('/login')
        }
    }
    next()
})

export default router